package pl.register.android.activities.gallery;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import com.actionbarsherlock.view.MenuItem;
import pl.register.android.R;
import pl.register.android.activities.gallery.logic.AsyncImagesLoader;
import pl.register.android.activities.gallery.ui.GalleryAdapter;
import pl.register.android.activities.gallery.ui.StudentGalleryView;
import pl.register.android.gallery.LoadedImage;
import pl.register.android.roboguice.activity.RoboSherlockActivity;
import pl.register.android.storage.external.CheckerSDCard;
import pl.register.android.storage.external.CourseRegisterSDCardManager;
import pl.register.android.student.Student;
import pl.register.android.util.StringUtils;
import roboguice.inject.ContentView;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-04
 */
@ContentView(R.layout.student_gallery_layout)
public class StudentGalleryActivity extends RoboSherlockActivity implements AdapterView.OnItemClickListener, StudentGalleryView {

    public static final String EXTRA_STUDENT_ID = "student_gallery_id_key";

    private static final String FILE_TYPE = "image/*";

    @InjectView(R.id.student_gallery_grid_view)
    private GridView gvGallery;

    private GalleryAdapter galleryAdapter;

    @InjectExtra(EXTRA_STUDENT_ID)
    private Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        onCreateActionBar(student);
        setupContent();

        if (CheckerSDCard.isAvailable(this)) {
            CourseRegisterSDCardManager.checkStudentGallery(this, student);
            new AsyncImagesLoader(this, student, this).execute();
        }
    }

    private void onCreateActionBar(Student student) {
        getSupportActionBar().setIcon(R.drawable.ic_gallery_white);
        getSupportActionBar().setTitle(StringUtils.build(student.getFirstName(), " ", student.getLastName()));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setupContent() {
        galleryAdapter = new GalleryAdapter(this);
        gvGallery.setAdapter(galleryAdapter);
        gvGallery.setOnItemClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        LoadedImage image = galleryAdapter.getItem(position);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(image.getUri(), FILE_TYPE);
        startActivity(intent);
    }

    @Override
    public void addImage(LoadedImage... images) {
        if (galleryAdapter == null) {
            return;
        }
        for (LoadedImage image : images) {
            galleryAdapter.addImage(image);
        }
    }

}